package controllers;

import helpers.UrlHelper;

import javax.servlet.ServletException;
import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;

import systems.ConfigSystem;

import entities.InLink;
import entities.SiteConfig;

import models.InLinkModel;


public class InLinkServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String name = URLDecoder.decode(path.toLowerCase().replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		if (name == null || name.isEmpty() || !UrlHelper.checkDomain(name)) {
			response.sendRedirect("/msg/page-not-found");
			return;
		}
		
		InLinkModel inlinkModel = InLinkModel.getInstance();
		InLink inlink = inlinkModel.get(name);
		
		if (inlink == null) {
			response.sendRedirect("/msg/page-not-found");
			return;
		}
		
		//Cache header
		response.addHeader("Cache-Control", "public, max-age=60000");
		
		request.setAttribute("inlink", inlink);
		try {
			request.getRequestDispatcher("/pages/inlink.jsp").forward(request, response);
		} catch (ServletException e) {
			//e.printStackTrace();
		}
	}
}
